package com.ncmmall.mall.controller.business.distribution;

import com.ncmmall.application.business.distribution.DistributorApplication;
import com.feijin.commons.exceptions.IllegalRequestException;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.business.distribution.DistributionCheck;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Samuel
 * @description 分销商审核
 * @date 2020-08-25 10:54
 */

@Controller
@RequestMapping("/distributor/check")
@Slf4j
public class DistributorCheckController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private DistributorApplication distributorApplication;

    /**
     * 分销商审核列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, DistributionCheck params) {

        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageNo).setPageSize(pageSize);
        String hql = "select d from DistributionCheck d where 1 = 1";
        Map<String, Object> map = new HashMap<>();
        hql = buildHQL(hql, params, map);

        QueryPage<DistributionCheck> page = queryChannelService.findPage(hql, pageRequest.setParams(map), DistributionCheck.class);
        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/distribution/check/list";
    }

    private String buildHQL(String hql, DistributionCheck params, Map<String, Object> map) {

        WebUser user = params.getMember();
        if (user != null) {
            if (StringUtils.isNotBlank(user.getMobile())) {
                hql += " and d.member.mobile like :mobile";
                map.put("mobile", "%" + user.getMobile() + "%");
            }

            if (StringUtils.isNotBlank(user.getRealName())) {
                hql += " and d.member.realName like :realName";
                map.put("realName", "%" + user.getRealName() + "%");
            }
        }

        if (params.getCheckStatus() > 0){
            hql += " and d.checkStatus = :status";
            map.put("status", params.getCheckStatus());
        }

        hql += " order by d.createTime desc";

        return hql;
    }

    /**
     * 审核分销商
     */
    @RequestMapping(value = "/{id}/check", method = RequestMethod.GET)
    private String check(Model model, @PathVariable("id") long id) {

        DistributionCheck distributionCheck = queryChannelService.findOne(DistributionCheck.class, id);
        model.addAttribute("distributionCheck", distributionCheck)
                .addAttribute("_method", "PUT");

        return "mall/distribution/check/check";
    }

    /**
     * 审核分销商
     */
    @RequestMapping(value = "/{id}/check", method = RequestMethod.PUT)
    @ResponseBody
    private Map<String, Object> check(@RequestHeader("X-Requested-With") String requestWith, @ModelAttribute DistributionCheck distributionCheck) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalRequestException("request must be ajax !");
            }

            distributorApplication.check(distributionCheck);

            return ImmutableMap.of("status", true);
        } catch (Exception e) {
            log.error("[审核分销商异常]", e);
            return ImmutableMap.of("status", false);
        }
    }

    /**
     * 批量审核
     */
    @RequestMapping(value = "/allCheck", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> allCheck(@RequestHeader("X-Requested-With") String requestWith, String data) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }

            distributorApplication.allCheck(data);
            return ImmutableMap.of("status", true, "message", "审核成功");

        } catch (Exception e) {

            log.error("批量审核异常", e);
            return ImmutableMap.of("status", false, "message", "审核异常");

        }
    }
}
